﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.IO;
namespace TTCaller
{
    class LogManager
    {
        int m_nLogMax = 500;
        Queue m_qLog;
        static private LogManager m_Instance = new LogManager();

        private LogManager()
        {
            m_qLog = new Queue();

        }


        static public LogManager getInstance()
        {
            return m_Instance;
        }

        public void Log(string arg_strLog, bool arg_bShowMsg)
        {
            Log(arg_strLog);

            if(arg_bShowMsg)
                System.Windows.Forms.MessageBox.Show(arg_strLog);
        }

        public void Log(string arg_strLog)
        {
            //if (StoreData.getInstance().IsDebug)
            {
                lock (m_qLog)
                {
                    m_qLog.Enqueue(System.DateTime.Now.ToString("HH:mm:ss#") + arg_strLog);
                }
                 
                //Form_Main.Main.Log(System.DateTime.Now.ToString("HH:mm:ss#") + arg_strLog);
                checkCount();


            }

        }
        public void checkCount()
        {
            lock (m_qLog)
            {
                if (m_qLog.Count > m_nLogMax)
                {
                    for (int i = 0; i < 50; i++)
                        m_qLog.Dequeue();
                }
            }


        }

        public void Log(string arg_strLog, Exception arg_ex)
        {
            Log(arg_strLog);
            Log(arg_ex.Message);
            Log(arg_ex.StackTrace);
            //System.Windows.Forms.MessageBox.Show(arg_strLog + arg_ex.Message);

            TextWriter tw = null;
            try
            {
                tw = new StreamWriter(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName) + "\\Log.txt", true);

                Queue qTemp = (Queue)m_qLog.Clone();

                string strLog = (string)qTemp.Dequeue();



                while (strLog != "")
                {
                    if (qTemp.Count < 20)
                    {
                        tw.WriteLine(strLog);
                    }
                    strLog = (string)qTemp.Dequeue();
                }


            }
            catch (Exception ex2)
            {
                //MessageBox.Show(ex2.Message);
            }
            finally
            {
                if (tw != null)
                    tw.Close();
            }
        }

        public int LogCount()
        {
            lock (m_qLog)
            {
                return m_qLog.Count;
            }
        }

        public string getLog()
        {
            lock (m_qLog)
            {
                string strResult = "";
                if (m_qLog.Count > 0)
                    strResult = (string)m_qLog.Dequeue();
                return strResult;
            }

        }
        

    }
}
